#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re;
from pyquery import PyQuery as pq;
import pymongo;
from  config import *
# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
browser=webdriver.Chrome()
# browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10);
client=pymongo.MongoClient(MONGO_URL);
db=client["taobao"];
MONGO_TABLE="飞科0519"

def search():
    try:
        browser.get("https://www.taobao.com");
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#q"))
        );
        click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button')));
        input.send_keys("飞科剃须刀");
        click.click();

        total = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.total"))
        );
        getProducts();
        return total.text;
    except TimeoutException:
        print("search timeout")
        return search();

def next_page(number):
    print(number)
    try:
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > input"))
        );
        click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')));
        input.clear();
        input.send_keys(number);
        click.click();

        wait.until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(number)
            )
        );
        getProducts();
    except TimeoutException:
        print("next page timeout");
        next_page(number);

def getProducts():
    total = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-itemlist .items .item"))
    );
    html=browser.page_source;
    doc=pq(html);
    items=doc("#mainsrp-itemlist .items .item").items();
    for item in items:
        product={
            "title":item.find(".title").text(),
            "img":item.find(".pic .img").attr("src"),
            "price":item.find(".price").text(),
            "deal":item.find(".deal-cnt").text()[:-3],
            "shop":item.find(".shop").text(),
            "location":item.find(".location").text()
        }
        print(product)
        saveToMongo(product);


def main():
    text=search();
    total=int(re.compile("(\d+)").search(text).group(1));
    for i in range(2,total+1):
        next_page(i);


def saveToMongo(result):
    try:
        if db[MONGO_TABLE].insert(result):
            print("save result success");
    except:
        print("save mongo error")


if __name__ == '__main__':
    main();
    